package com.ruoyi.report_wjjhl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.dynamic.datasource.annotation.DSTransactional;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.syrw.annotation.FIllBasicInfo;
import com.ruoyi.report_wjjhl.domain.SnhshjlBaDetail;
import com.ruoyi.report_wjjhl.domain.vo.SnhshjlBaDetailVo;
import com.ruoyi.report_wjjhl.mapper.SnhshjlBaDetailMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.report_wjjhl.domain.bo.SnhshjlBsInfoBo;
import com.ruoyi.report_wjjhl.domain.vo.SnhshjlBsInfoVo;
import com.ruoyi.report_wjjhl.domain.SnhshjlBsInfo;
import com.ruoyi.report_wjjhl.mapper.SnhshjlBsInfoMapper;
import com.ruoyi.report_wjjhl.service.ISnhshjlBsInfoService;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 水泥或石灰剂量试验检测记录EDTA滴定法Service业务层处理
 *
 * @author zyb
 * @date 2024-08-02
 */
@RequiredArgsConstructor
@Service
@DSTransactional
public class SnhshjlBsInfoServiceImpl implements ISnhshjlBsInfoService {

    private final SnhshjlBsInfoMapper baseMapper;
    private final SnhshjlBaDetailMapper detailMapper;

    /**
     * 查询水泥或石灰剂量试验检测记录EDTA滴定法
     */
    @Override
    @FIllBasicInfo
    public SnhshjlBsInfoVo queryById(Long excelId){
        SnhshjlBsInfoVo snhshjlBsInfoVo = baseMapper.selectVoById(excelId);
        // 查询detail
        List<SnhshjlBaDetailVo> baDetailVos = detailMapper.selectVoList(new LambdaQueryWrapper<SnhshjlBaDetail>()
            .eq(SnhshjlBaDetail::getExcelId, excelId));
        if (CollUtil.isNotEmpty(baDetailVos)) {
            snhshjlBsInfoVo.setBaDetailVos(baDetailVos);
        }
        return snhshjlBsInfoVo;
    }

    /**
     * 查询水泥或石灰剂量试验检测记录EDTA滴定法列表
     */
    @Override
    public TableDataInfo<SnhshjlBsInfoVo> queryPageList(SnhshjlBsInfoBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<SnhshjlBsInfo> lqw = buildQueryWrapper(bo);
        Page<SnhshjlBsInfoVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询水泥或石灰剂量试验检测记录EDTA滴定法列表
     */
    @Override
    public List<SnhshjlBsInfoVo> queryList(SnhshjlBsInfoBo bo) {
        LambdaQueryWrapper<SnhshjlBsInfo> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<SnhshjlBsInfo> buildQueryWrapper(SnhshjlBsInfoBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<SnhshjlBsInfo> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getTotalPage() != null, SnhshjlBsInfo::getTotalPage, bo.getTotalPage());
        lqw.eq(bo.getPage() != null, SnhshjlBsInfo::getPage, bo.getPage());
        lqw.eq(StringUtils.isNotBlank(bo.getJilbh()), SnhshjlBsInfo::getJilbh, bo.getJilbh());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancdwmc()), SnhshjlBsInfo::getJiancdwmc, bo.getJiancdwmc());
        lqw.eq(StringUtils.isNotBlank(bo.getGongcmc()), SnhshjlBsInfo::getGongcmc, bo.getGongcmc());
        lqw.eq(StringUtils.isNotBlank(bo.getGongcbwyt()), SnhshjlBsInfo::getGongcbwyt, bo.getGongcbwyt());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpmc()), SnhshjlBsInfo::getYangpmc, bo.getYangpmc());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpbh()), SnhshjlBsInfo::getYangpbh, bo.getYangpbh());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpsl()), SnhshjlBsInfo::getYangpsl, bo.getYangpsl());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpzt()), SnhshjlBsInfo::getYangpzt, bo.getYangpzt());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpsj()), SnhshjlBsInfo::getYangpsj, bo.getYangpsj());
        lqw.eq(StringUtils.isNotBlank(bo.getShiyjcrq()), SnhshjlBsInfo::getShiyjcrq, bo.getShiyjcrq());
        lqw.eq(StringUtils.isNotBlank(bo.getShiytjwd()), SnhshjlBsInfo::getShiytjwd, bo.getShiytjwd());
        lqw.eq(StringUtils.isNotBlank(bo.getShiytjsd()), SnhshjlBsInfo::getShiytjsd, bo.getShiytjsd());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancyj()), SnhshjlBsInfo::getJiancyj, bo.getJiancyj());
        lqw.eq(StringUtils.isNotBlank(bo.getPandyj()), SnhshjlBsInfo::getPandyj, bo.getPandyj());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancyjIds()), SnhshjlBsInfo::getJiancyjIds, bo.getJiancyjIds());
        lqw.eq(StringUtils.isNotBlank(bo.getPandyjIds()), SnhshjlBsInfo::getPandyjIds, bo.getPandyjIds());
        lqw.eq(StringUtils.isNotBlank(bo.getZhuyyqsbmcjbh()), SnhshjlBsInfo::getZhuyyqsbmcjbh, bo.getZhuyyqsbmcjbh());
        lqw.eq(StringUtils.isNotBlank(bo.getShebIds()), SnhshjlBsInfo::getShebIds, bo.getShebIds());
        lqw.eq(StringUtils.isNotBlank(bo.getFujsm()), SnhshjlBsInfo::getFujsm, bo.getFujsm());
        lqw.eq(StringUtils.isNotBlank(bo.getHuigfcs()), SnhshjlBsInfo::getHuigfcs, bo.getHuigfcs());
        lqw.eq(bo.getCansa() != null, SnhshjlBsInfo::getCansa, bo.getCansa());
        lqw.eq(bo.getCansb() != null, SnhshjlBsInfo::getCansb, bo.getCansb());
        lqw.eq(bo.getCansc() != null, SnhshjlBsInfo::getCansc, bo.getCansc());
        lqw.eq(bo.getShiycs() != null, SnhshjlBsInfo::getShiycs, bo.getShiycs());
        lqw.eq(bo.getZuidz() != null, SnhshjlBsInfo::getZuidz, bo.getZuidz());
        lqw.eq(bo.getZuixz() != null, SnhshjlBsInfo::getZuixz, bo.getZuixz());
        lqw.eq(bo.getPingjz() != null, SnhshjlBsInfo::getPingjz, bo.getPingjz());
        lqw.eq(bo.getBiaozc() != null, SnhshjlBsInfo::getBiaozc, bo.getBiaozc());
        lqw.eq(bo.getPiancxs() != null, SnhshjlBsInfo::getPiancxs, bo.getPiancxs());
        return lqw;
    }

    /**
     * 新增水泥或石灰剂量试验检测记录EDTA滴定法
     */
    @Override
    public Boolean insertByBo(SnhshjlBsInfoBo bo) {
        SnhshjlBsInfo add = BeanUtil.toBean(bo, SnhshjlBsInfo.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setExcelId(add.getExcelId());
            // 新增详细 10次
            ArrayList<SnhshjlBaDetail> details = new ArrayList<>(10);
            for (long i = 1; i <= 10L; i++) {
                details.add(new SnhshjlBaDetail(add.getExcelId()));
            }
            detailMapper.insertBatch(details);
        }
        return flag;
    }

    /**
     * 修改水泥或石灰剂量试验检测记录EDTA滴定法
     */
    @Override
    public Boolean updateByBo(SnhshjlBsInfoBo bo) {
        SnhshjlBsInfo update = BeanUtil.toBean(bo, SnhshjlBsInfo.class);
        validEntityBeforeSave(update);
        boolean flag = baseMapper.updateById(update) > 0;
        if (flag) {
            detailMapper.updateBatchById(BeanUtil.copyToList(bo.getBaDetailVos(), SnhshjlBaDetail.class));
        }
        return flag;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(SnhshjlBsInfo entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除水泥或石灰剂量试验检测记录EDTA滴定法
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
